﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MindFactorial.LaunchPoint.Common
{
    /// <summary>
    /// A concrete representation in GUID (Globally Unique Identifier) format of a system activity
    /// </summary>
    public struct ActivityID
    {
        /// <summary>
        /// the unique id of an activity
        /// </summary>
        public Guid UID;
    }

    /// <summary>
    /// represents a single system activity.  This is the smallest unit of work
    /// that can be secured in the framework
    /// </summary>
    internal class Activity : INamedObject<ActivityID>
    {
        ActivityID _id = default(ActivityID);
        string _description = null;
        internal ActivityID ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }

        public string Description
        {
            get
            {
                return _description;
            }
        }

        public ActivityID Name
        {
            get { return _id; }
        }

       
    }

    /// <summary>
    /// Collection of activityies associated with a role
    /// </summary>
    internal class ActivityCollection : GenericNamedObjectDictionary<ActivityID, Activity>
    {
        internal bool Contains(ActivityID id)
        {
            return _items.ContainsKey(id);           
        }
    }


    /// <summary>
    /// Represents a given role
    /// </summary>
    internal class Role : INamedObject<string>
    {
        ActivityCollection _activities = null;
        string _name = null;

        internal Role()
        {
            _activities = new ActivityCollection();
        }

        /// <summary>
        /// checks to see if the role has the given activity defined
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasActivity(ActivityID id)
        {
            
            return false;
        }

        #region INamedObject<string> Members
        /// <summary>
        /// gets the name of the role
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        #endregion
    }

    /// <summary>
    /// collection of roles associated with a given RuntimeUser
    /// </summary>
    internal class RoleCollection : GenericNamedObjectDictionary<string, Role>
    {
        public bool HasRoleWithActivity(ActivityID id)
        {
            foreach (Role role in this._items.Values)
            {
                if (role.HasActivity(id))
                    return true;
            }
            return false;
        }
    }

    /// <summary>
    /// The Runtime User singleton for any given instance of the LaunchPoint runtime
    /// </summary>
    public class RuntimeUser
    {
        string _username = null, _password = null;
        RoleCollection _roles = null;
        RuntimeUser _user = null;

        /// <summary>
        /// Creates a new Runtime User
        /// </summary>
        internal RuntimeUser()
        {
            _username = "";
            _password = "";
           
        }

        /// <summary>
        /// gets the username
        /// </summary>
        public string Username
        {
            get
            {
                return _username;
            }
        }

        /// <summary>
        /// gets the password
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }
        }

        /// <summary>
        /// Determines whether the currently logged on user
        /// is allowed to perfrom this activity
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsInRoleForActivity(ActivityID id)
        {
            //TODO; authorize based on activityid
            return _user._roles.HasRoleWithActivity(id) ;
        }

        /// <summary>
        /// Attempts to log in the user with the specified username and password
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string username, string password)
        {
            RuntimeUser ru = new RuntimeUser();
           
            //TODO:
            //query for user in database and authenticate
            //instantiate object
            //get roles for user and put in roles collection
            return true;
        }

        /// <summary>
        /// logs out the currently logged in user
        /// </summary>
        public void Logout()
        {

        }
    }
}
